package com.xyht.sca_s.student_manage_system.modules.schoolEditor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.schoolEditor.entity.SmsSchoolEditor;
import com.xyht.sca_s.student_manage_system.modules.schoolEditor.entity.dto.SmsSchoolEditorAddDto;
import com.xyht.sca_s.student_manage_system.modules.schoolEditor.entity.dto.SmsSchoolEditorUpdateDto;
import com.xyht.sca_s.student_manage_system.modules.schoolEditor.entity.vo.SmsSchoolEditorVo;
import com.xyht.sca_s.student_manage_system.modules.schoolEditor.mapper.SmsSchoolEditorMapper;
import com.xyht.sca_s.student_manage_system.modules.schoolEditor.service.SmsSchoolEditorService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2022-12-25
 */
@Service
public class SmsSchoolEditorServiceImpl extends ServiceImpl<SmsSchoolEditorMapper, SmsSchoolEditor> implements SmsSchoolEditorService {

    @Resource
    private SmsSchoolEditorMapper smsSchoolEditorMapper;
    @Override
    @Transactional
    public ResponseResult addSchoolEditor(SmsSchoolEditorAddDto smsSchoolEditorAddDto) {
        //先清空数据
        QueryWrapper<SmsSchoolEditor> queryWrapper = new QueryWrapper<>();
        int result = smsSchoolEditorMapper.delete(queryWrapper);
        if(result == 0){
            return CommonResult.failed(CommonCodeEnum.DELETE_FAIL);
        }
        //再判断参数 新增
        if(isNullOrEmpty(smsSchoolEditorAddDto.getSchoolName()) || isNullOrEmpty(smsSchoolEditorAddDto.getSchoolAddress())
        || isNullOrEmpty(smsSchoolEditorAddDto.getSchoolProfile()) || isNullOrEmpty(smsSchoolEditorAddDto.getLogo())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsSchoolEditor smsSchoolEditor = new SmsSchoolEditor();
        BeanUtils.copyProperties(smsSchoolEditorAddDto,smsSchoolEditor);
        smsSchoolEditor.setCreateTime(new Date());
        smsSchoolEditorMapper.insert(smsSchoolEditor);
        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult updateSchoolEditor(SmsSchoolEditorUpdateDto smsSchoolEditorUpdateDto) {
        //先清空数据
        QueryWrapper<SmsSchoolEditor> queryWrapper = new QueryWrapper<>();
        int deleteResult = smsSchoolEditorMapper.delete(queryWrapper);
        if(deleteResult == 0){
            return CommonResult.failed(CommonCodeEnum.DELETE_FAIL);
        }
        //再判断参数 修改数据
        if(isNullOrEmpty(smsSchoolEditorUpdateDto.getSchoolName()) || isNullOrEmpty(smsSchoolEditorUpdateDto.getSchoolAddress())
                || isNullOrEmpty(smsSchoolEditorUpdateDto.getSchoolProfile()) || isNullOrEmpty(smsSchoolEditorUpdateDto.getLogo())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsSchoolEditor smsSchoolEditor = new SmsSchoolEditor();
        BeanUtils.copyProperties(smsSchoolEditorUpdateDto,smsSchoolEditor);
        smsSchoolEditor.setUpdateTime(new Date());
        int result = smsSchoolEditorMapper.insert(smsSchoolEditor);
        if(result == 0){
            return CommonResult.failed(CommonCodeEnum.UPDATE_FAIL);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult selectSchoolEditor() {
        QueryWrapper<SmsSchoolEditor> queryWrapper = new QueryWrapper<>();
        SmsSchoolEditor smsSchoolEditor = smsSchoolEditorMapper.selectOne(queryWrapper);
        if(smsSchoolEditor == null){
           return CommonResult.failed(CommonCodeEnum.SCHOOL_EDITOR_NOT_EXIST);
        }
        SmsSchoolEditorVo smsSchoolEditorVo = new SmsSchoolEditorVo();
        BeanUtils.copyProperties(smsSchoolEditor,smsSchoolEditorVo);
        return CommonResult.success(smsSchoolEditorVo);
    }
}
